﻿<?PHP
require_once("base.wpfb.php");

class Settings {

private $globalVars = null;
private $entryPageName = null;

public function __construct($entryPageName) {
	$this->entryPageName = $entryPageName;
}

private function customizedUpdateOption($option_name, $new_value)
{
	if ( get_option( $option_name ) != $new_value ) {
		update_option( $option_name, $new_value );
	} else {
		$deprecated = ' ';
		$autoload = 'no';
		add_option( $option_name, $new_value, $deprecated, $autoload );
	}
}

public function renderHTMLforGroup(){
	if(isset($_GET['deleteData']))
	{
		$this->globalVars = new WPFB(plugin_dir_url(__FILE__));
		$this->globalVars->truncate_CBMBCNIOR513W2S();
		echo '<h1>Datatables are properly emptied</h1><hr/>';
	}
	$this->renderHTMLforTerminal();
?>
	<a href="<?PHP bloginfo('wpurl');?>/wp-admin/admin.php?page=<?PHP echo $this->entryPageName; ?>&deleteData=true">Delete all database entry</a>
<?PHP
}

public function renderHTMLforTerminal(){
	try{
?>
		<div class="wrap">
		<h1>Set your API KEY here</h1>
		<small>You can get your API keys here: <a target='_blank' href='https://developers.facebook.com/apps'>https://developers.facebook.com/apps</a></small>
		<br/><br/><br/>
<?PHP
		if( isset($_POST['wpfb_key']) )
		{
			$localAPI_KEY = $_POST['wpfb_key'];
			$localAPI_KEY = trim($localAPI_KEY);
			$this->customizedUpdateOption( 'wpfb_KEY', $localAPI_KEY);
		}	
		if( isset($_POST['wpfb_secret']) )
		{
			$localSECRET = $_POST['wpfb_secret'];
			$localSECRET = trim($localSECRET);
			$this->customizedUpdateOption( 'wpfb_SECRET', $localSECRET);
		}
?>
		<form enctype="multipart/form-data" action="<?php echo get_bloginfo('wpurl') . '/wp-admin/admin.php?page=' . $this->entryPageName ?>" method="POST">
		API KEY: <input type="text" class="regular-text" name="wpfb_key" value="<?PHP echo get_option('wpfb_KEY'); ?>" /><br/>
		SECRET: <input type="text" class="regular-text" name="wpfb_secret" value="<?PHP echo get_option('wpfb_SECRET'); ?>" /><br/><br/>
		<input type="submit" class="submit button button-primary" value="Submit">
		</form>

		<br/>
		<br/>
		<br/>
		</div>
<?PHP
	}catch (Exception $e) {
		echo "<h3>Caught exception: <font style='color:red'>{$e->getMessage()}</font></h3><hr/>";
	} // try-catch for whole window ends here
}

private function deleteData()
{
	try{
		$this->globalVars = new WPFB(plugin_dir_url(__FILE__));
		$this->globalVars->truncate_CBMBCNIOR513W2S();
		echo '<h1>Datatables are properly emptied</h1><hr/>';
	}catch (Exception $e) {
		$this->globalVars->printException($e);
	} // try-catch for whole window ends here
} // delete all Data from database;

} // class ends here
?>